package com.rightsidetech.http.controller.agentAdmin.trade;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.trade.dtos.request.*;
import com.rightsidetech.trade.dtos.response.*;
import com.rightsidetech.trade.service.TdCommodityUserOrderService;
import com.rightsidetech.user.dtos.requst.IdReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

@RestController
@RequestMapping("/agentAdmin/trade")
@ShenyuSpringMvcClient(path = "/agentAdmin/trade/**")
@Api(tags = "代理商:用户订单相关")
public class TdCommodityAgentTradeController extends BaseController {

    @Resource
    private TdCommodityUserOrderService tdCommodityUserOrderService;
    @Resource
    private SysAgentInfoService sysAgentInfoService;

    @Log
    @Token
    @ApiOperation("代理商订单列表")
    @Auth(bizType = Auth.AGENT)
    @GetMapping("/page")
    @ApiOperationSupport(ignoreParameters = {"agentId","areaId"})
    public ResponseData<PageInfo<TdCommodityUserOrderAgentResp>> agentPage(TdCommodityOrderListRequest req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null){
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(),validate.getMessage());
        }
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        req.setAgentId(agentInfoId);
        return tdCommodityUserOrderService.agentPage(req);
    }

    @Log
    @Token
    @ApiOperation("用户订单详细")
    @Auth(bizType = Auth.AGENT)
    @GetMapping("/detail")
    public ResponseData<TdCommodityDetailResp> getByOrderId(@ApiParam(required = true, name = "id", value = "id") @RequestParam("id") Long id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"id不能为空");
        }

        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        return tdCommodityUserOrderService.getByOrderIdAndAgentId(id, agentInfoId);
    }


    @Log
    @Token
    @ApiOperation("商城账单")
    @Auth(bizType = Auth.AGENT)
    @GetMapping("/billPage")
    @ApiOperationSupport(ignoreParameters = {"agentId","areaId"})
    public ResponseData<PageInfo<TdCommodityTradeOrderResp>> findOrderBillPage(TdCommodityTradeOrderReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

//        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
//        req.setAgentId(agentInfoId);

        return tdCommodityUserOrderService.listTradeOrder(req);
    }

    @Log
    @Token
    @ApiOperation("订单售后列表")
    @Auth(bizType = Auth.AGENT)
    @GetMapping("/afterSale")
    public ResponseData<PageInfo<TdCommodityAfterPageResp>> afterServicePage(TdCommodityAfterServiceReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        req.setAgentId(agentInfoId);

        return tdCommodityUserOrderService.afterServicePage(req);
    }

    @Log
    @Token
    @ApiOperation("订单售后退款")
    @Auth(bizType = Auth.AGENT)
    @GetMapping("/afterSaleMoney")
    @ApiOperationSupport(ignoreParameters = {"areaId","agentId"})
    public ResponseData<PageInfo<TdCommodityAfterServiceMoneyResp>> afterServiceMoneyAgentPage(TdCommodityAfterServiceMoneyReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        req.setAgentId(agentInfoId);

        return tdCommodityUserOrderService.afterServiceMoneyPage(req);
    }

    @Log
    @Token
    @ApiOperation("订单售后详情")
    @Auth(bizType = Auth.AGENT)
    @GetMapping("/afterSale_Detail")
    public ResponseData<TdCommodityAfterServiceDetailResp> findAfterServiceDetail(IdReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        return tdCommodityUserOrderService.findAfterServiceDetailById(req.getId());
    }

}
